﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsServer
    {
        private readonly KeRui.DAL.CmsServer dal = new KeRui.DAL.CmsServer();

        public void Add(KeRui.Model.CmsServer model)
        {
            this.dal.Add(model);
        }

        public List<KeRui.Model.CmsServer> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsServer> list = new List<KeRui.Model.CmsServer>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsServer item = new KeRui.Model.CmsServer();
                    if (dt.Rows[i]["ServerID"].ToString() != "")
                    {
                        item.ServerID = int.Parse(dt.Rows[i]["ServerID"].ToString());
                    }
                    item.ServerName = dt.Rows[i]["ServerName"].ToString();
                    item.ServerIP = dt.Rows[i]["ServerIP"].ToString();
                    item.ServerUrl = dt.Rows[i]["ServerUrl"].ToString();
                    item.ServerKey = dt.Rows[i]["ServerKey"].ToString();
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        item.IsUsing = int.Parse(dt.Rows[i]["IsUsing"].ToString());
                    }
                    if (dt.Rows[i]["LicenseID"].ToString() != "")
                    {
                        item.LicenseID = int.Parse(dt.Rows[i]["LicenseID"].ToString());
                    }
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    item.Domain = dt.Rows[i]["Domain"].ToString();
                    if (dt.Rows[i]["SiteNum"].ToString() != "")
                    {
                        item.SiteNum = int.Parse(dt.Rows[i]["SiteNum"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int ServerID)
        {
            return this.dal.Delete(ServerID);
        }

        public List<KeRui.Model.CmsServer> GetList(int IsUsing)
        {
            string strWhere = "";
            if ((IsUsing == 0) || (IsUsing == 1))
            {
                strWhere = strWhere + " IsUsing=" + IsUsing;
            }
            DataSet set = new KeRui.DAL.Common().GetList("CmsServer", -1, -1, strWhere, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public int GetMaxOrderID()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetMaxOrderID(" CmsServer ", " OrderID ");
        }

        public KeRui.Model.CmsServer GetModel(int ServerID)
        {
            return this.dal.GetModel(ServerID);
        }

        public KeRui.Model.CmsServer GetModel(string ServerIP)
        {
            return this.dal.GetModel(ServerIP);
        }

        public KeRui.Model.CmsServer GetModelByCache(int ServerID)
        {
            string cacheKey = "CmsServerModel-" + ServerID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ServerID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsServer) cache;
        }

        public KeRui.Model.CmsServer GetModelByCache(string ServerIP)
        {
            string cacheKey = "CmsServerModel-" + ServerIP;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ServerIP);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsServer) cache;
        }

        public DataSet GetSel(int LicenseID)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            string strWhere = " (ServerIP not in (SELECT ServerIP FROM CmsServer) and IsActivated=1 )";
            if (LicenseID != 0)
            {
                strWhere = strWhere + "or LicenseID = " + LicenseID;
            }
            return common.GetSelect("SysLicense ", strWhere, " LicenseID ", " LicenseID,ServerIP ");
        }

        public DataSet GetSelect()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            string strWhere = " ";
            return common.GetSelect("CmsServer ", strWhere, " ServerID ", " ServerUrl,ServerName ");
        }

        public DataSet GetSelect1()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            string strWhere = " ";
            return common.GetSelect("CmsServer ", strWhere, " ServerID ", " ServerID,ServerName ");
        }

        public int GetServerIDByIP(string IP)
        {
            return this.dal.GetServerIDByIP(IP);
        }

        public int GetServerIDByUrl(string ServerUrl)
        {
            return this.dal.GetServerIDByUrl(ServerUrl);
        }

        public void Update(KeRui.Model.CmsServer model)
        {
            this.dal.Update(model);
        }
    }
}

